<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台banner图片管理控制器
 * @author 明明 <zhumingming_12346@163.com>
 */
class BannerController extends AdminController {

    /**
     * 后台等级管理
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        if(UID){
            $this->meta_title = 'Banner图片';
            $this->met_title = 'Banner图片管理';
            $grade=M('Banner')->select();
            int_to_string($grade);
           $this->assign('list',$grade);
            $this->display();
        } else {
            $this->redirect('Public/login');
        }
    }

    /***
     * 添加轮播图
     */
    public function add(){
        $this->meta_title = '添加轮播图';
        if($_POST){
          if(empty($_POST['title'])){
              $this->error('轮播名称不能为空');
          }
            if(empty($_POST['path'])){
                $this->error('请上传图片');
            }
            $_POST['addtime']=time();
           $user= M('Banner')->add($_POST);
            if(!$user){
                $this->error('轮播图添加失败');
            }else{
                $this->success('轮播图添加成功',U('Banner/index'));
            }
        }
        $this->display('edit');
    }

    /**
     * 上传banner图片
     *
     */
    public function uploadPicture(){
        /* 返回标准数据 */
        $size=$_FILES['download']['size'];
        if($size >'5242880'){
            $return['status'] = 0;
            $return['info'] ='上传图片不能超过5M';
            $this->ajaxReturn($return);
        }
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        );
        if($info){
            $return['status'] = 1;
            $return = array_merge($info['download'], $return);
        } else {
            $return['status'] = 0;
            $return['info']   = $Picture->getError();
        }
        $this->ajaxReturn($return);
    }


    /**
     * 上传商品图片
     *
     */
    public function uploadPath(){
        /* 返回标准数据 */
        $size=$_FILES['download']['size'];
        if($size >'5242880'){
            $return['status'] = 0;
            $return['info'] ='上传图片不能超过5M';
            $this->ajaxReturn($return);
        }
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        );
        if($info){
            $return['status'] = 1;
            $return['path']=$info['Filedata']['path'];
            $return['src']=$info['Filedata']['id'];
        } else {
            $return['status'] = 0;
            $return['info']   = $Picture->getError();
        }
        $this->ajaxReturn($return);
    }
    /**
     * 删除商品图片
     */
    public function delPath(){
         $id=   I('id');
        $id=    M('Picture')->where(array('id'=>$id))->delete();
        if(empty($id)){
            $return['status'] = 0;
        }else{
            $return['status'] = 1;
        }
        $this->ajaxReturn($return);
    }





    /***
     *   删除banner 图片
     */
    public function dele(){
            $id=intval(I('id'));
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
       $del= M('Banner')->where(array('id'=>$id))->delete();
        if(!$del){
            $this->error('轮播图删除失败,请重试');
        }else{
            $this->success('轮播图删除成功',U('Banner/index'));
        }
    }
}
